package cn.sjxy.apiClient;

import cn.sjxy.baseLib.AjaxResult;
import cn.sjxy.model.Md_User;
import cn.sjxy.service.Sv_Admin;
import cn.sjxy.service.Sv_Login;
import cn.sjxy.service.Sv_User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;

/**
 * 用户登录
 */
@RestController
@Validated
public class Ci_Login {

    @Autowired
    private Sv_Admin sv_admin;
    @Autowired
    private Sv_User sv_user;
    @Autowired
    private Sv_Login sv_login;

    /**
     * 用户登录
     *
     * @param userCode
     * @param password
     * @return
     */
    @PostMapping(value = "/login/userLogin")
    public AjaxResult userLogin(@NotBlank(message = "用户名必填") String userCode, @NotBlank(message = "密码必填") String password) throws Exception {
        Md_User user = sv_user.checkLogin(userCode, password);

        String token = sv_login.saveUser(userCode, password);
        AjaxResult result = AjaxResult.success("登录成功");
        result.addKey("token", token);
        return result;
    }

    /**
     * 用户注册
     *
     * @param userCode
     * @param userName
     * @param password
     * @param userSex
     * @return
     */
    @PostMapping(value = "/login/userRegister")
    public AjaxResult userRegister(
            @NotBlank(message = "用户账号必填") String userCode,
            @NotBlank(message = "用户名称必填") String userName,
            @NotBlank(message = "密码必填") String password,
            String userSex) throws Exception {
        sv_user.register(userCode, userName, password, userSex);
        return AjaxResult.success("注册成功");
    }

    //退出
    @PostMapping(value = "/login/userLogout")
    public AjaxResult userLogout(String token) {
        sv_login.logout(token);
        return AjaxResult.success("退出成功");
    }
}
